package com.shiro.demo3.controller;

import com.shiro.demo3.utils.R;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/page")
public class PageController {

    @RequestMapping("/login")
    public String login(Model model){
        model.addAttribute("msg","欢迎登录");
        return "login";
    }
    @RequestMapping("/loginData")
    @ResponseBody
    public R loginData(@RequestParam String username,@RequestParam String password){

        try {
            Subject subject = SecurityUtils.getSubject();
            // password是明文123456
            AuthenticationToken token = new UsernamePasswordToken(username,password);
            subject.login(token);
            return R.success().msg("登录成功");
        } catch (UnknownAccountException e) {
            return R.fail().msg("账号错误");
        } catch (IncorrectCredentialsException e) {
            return R.fail().msg("密码错误");
        }
    }
    @RequestMapping("/success")
    public String success(){
        return "success";
    }
}
